package add_$11_Case_7_2_byMyself;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class FileUtil {
    
    public void saveGood(Good good) throws IOException {
        
        Date data = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String time = sdf.format(data);
        File file = new File("货存变动记录" + time + ".csv");
        BufferedOutputStream bos = null;
        
        System.out.println(file.exists() + "\n" + file.getAbsolutePath() + ",0");
        
        if (!file.exists()) {
            
            file.createNewFile();
            
            System.out.println(file.exists() + "\n" + file.getAbsolutePath() + ",-1");
            
            bos = new BufferedOutputStream(new FileOutputStream(file, true));
            
            bos.write("编号，书名，单价，库存，总价\r\n".getBytes());
            String goodInfo = good.getId() + ", " + good.getName() + ", " + good.getPrice() + ", " + good.getNumber() + ", " + good.getMoney();
            bos.write((goodInfo + "\r\n").getBytes());
            
        } else {
            
            System.out.println(file.exists() + "\n" + file.getAbsolutePath() + ",1");
            
            bos = new BufferedOutputStream(new FileOutputStream(file, true));
            
            bos.write("编号，书名，单价，库存，总价\r\n".getBytes());
            String goodInfo = good.getId() + ", " + good.getName() + ", " + good.getPrice() + ", " + good.getNumber() + ", " + good.getMoney();
            bos.write((goodInfo + "\r\n").getBytes());
        }
        bos.close();
    }

}
